import { RequestMethodEnum } from "../../req/httpEnum";
import { PaginationParam, PaginationResult } from "../../models";
import type { GatewayTypeItem, GatewayTypeAttrItem, GatewayPointItem, GatewayItem } from "./models";
import req, { RequsetOptons } from "/@/api/req";

export enum Api {
  //网关类型
  gatewayTypePage = "api/aiot/gatewayType/page",
  getManufacturerCode = "api/aiot/gatewayType/getManufacturerCode",
  addGatewayType = "api/aiot/gatewayType/addGatewayType",
  updateGatewayType = "api/aiot/gatewayType/updateGatewayType",
  deleteByIds = "api/aiot/gatewayType/deleteByIds",
  updateAttr = "api/aiot/gatewayTypeDatapointAttrs/saveOrUpdateGatewayTypeDatapointAttrs",
  getGatewayTypeAttrs = "api/aiot/gatewayTypeDatapointAttrs/page",
  deleteAttrByIds = "api/aiot/gatewayTypeDatapointAttrs/deleteByIds",
  copyGatewayType = "api/aiot/gatewayType/copyGatewayType",
  //平台网关类型
  defaultGatewayTypePage = "api/aiot/defaultGatewayTypeController/page",
  defaultGetManufacturerCode = "api/aiot/defaultGatewayTypeController/getManufacturerCode",
  addDefaultGatewayType = "api/aiot/defaultGatewayTypeController/addDefaultGatewayType",
  updateDefaultGatewayType = "api/aiot/defaultGatewayTypeController/updateDefaultGatewayType",
  deleteDefaultByIds = "api/aiot/defaultGatewayTypeController/deleteByIds",
  updateDefaultAttr = "api/aiot/defaultGatewayTypeDatapointAttrs/saveOrUpdateGatewayTypeDatapointAttrs",
  getDefaultGatewayTypeAttrs = "api/aiot/defaultGatewayTypeDatapointAttrs/page",
  deleteDefaultAttrByIds = "api/aiot/defaultGatewayTypeDatapointAttrs/deleteByIds",
  exportTemplate = "api/aiot/gatewayTypeDatapointAttrs/exportTemplate",
  importExcel = "api/aiot/gatewayTypeDatapointAttrs/uploadExcel",
  // importExcel = "http://192.168.13.1:30006/gatewayTypeDatapointAttrs/uploadExcel",
  //网关
  gatewayPage = "api/aiot/gateways/page",
  addGateway = "api/aiot/gateways/addGateways",
  updateGateway = "api/aiot/gateways/updateGateways",
  deleteGateways = "api/aiot/gateways/deleteGatewayByIds",
  addGatewayDatapoints = "api/aiot/gatewayDataPoints/addGatewayDatapoints",
  gatewayDataPointsPage = "api/aiot/gatewayDataPoints/page",
  deleteGatewayPoints = "api/aiot/gatewayDataPoints/deleteByIds",
  updateGatewayDatapoints = "api/aiot/gatewayDataPoints/updateGatewayDatapoints",
}

export function getManufacturerCode(isDefault: boolean) {
  return req.commonPost<{ value: string; text: string }[]>(
    isDefault ? Api.defaultGetManufacturerCode : Api.getManufacturerCode,
    null,
    { useGlobalLoading: false }
  );
}

export function updateAttr(isDefault: boolean, data: Partial<GatewayTypeAttrItem>[]) {
  return req.commonPost<boolean>(isDefault ? Api.updateDefaultAttr : Api.updateAttr, data);
}

export function addGatewayType(isDefault: boolean, data: Partial<GatewayTypeItem>) {
  return req.commonPost<GatewayTypeItem>(isDefault ? Api.addDefaultGatewayType : Api.addGatewayType, data);
}

export function deleteAttrByIds(isDefault: boolean, ids: string[]) {
  return req.commonPost<boolean>(isDefault ? Api.deleteDefaultAttrByIds : Api.deleteAttrByIds, ids);
}

export function addGatewayDatapoints(data: Partial<GatewayPointItem>) {
  return req.commonPost<GatewayPointItem>(Api.addGatewayDatapoints, data);
}

export function copyGatewayType(data: Partial<GatewayTypeItem>[]) {
  return req.commonPost<boolean>(Api.copyGatewayType, data);
}

export function exportTemplate(gatewayTypeId: string) {
  return req.export({
    url: Api.exportTemplate,
    method: RequestMethodEnum.GET,
    params: {
      gatewayTypeId,
    },
  });
}

export function importExcel(data: FormData) {
  return req.post<boolean>(
    {
      url: Api.importExcel,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}

export function gatewayPage(data: PaginationParam<GatewayItem>) {
  return req.commonPost<PaginationResult<GatewayItem>>(Api.gatewayPage, data);
}
export function getGatewayTypeAttrs(data: PaginationParam<GatewayTypeAttrItem>) {
  return req.commonPost<PaginationResult<GatewayTypeAttrItem>>(Api.getGatewayTypeAttrs, data);
}
// export function gatewayDataPointsPage(data: PaginationParam<GatewayTypeAttrItem>) {
//   return req.commonPost<PaginationResult<GatewayPointItem>>(Api.gatewayDataPointsPage, data);
// }

export function gatewayDataPointsPage(data: PaginationParam<GatewayTypeAttrItem>, useGlobalLoading = false) {
  return req.post<PaginationResult<GatewayPointItem>>(
    {
      url: Api.gatewayDataPointsPage,
      data,
    },
    {
      useGlobalLoading,
    }
  );
}
